package com.helloblog.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.hello.service.impl.CoreSerivceImpl;
import com.helloblog.domain.Blog;
import com.helloblog.domain.User;

public class BlogAction extends DispatchAction {

	
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String id = request.getParameter("blog_id");
		CoreSerivceImpl service = new CoreSerivceImpl();

		List list = service.getArticlesByBlogId(id);
		request.setAttribute("article_list", list);
		return mapping.findForward("blog_index");
	}

	public ActionForward open(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		try {
			if(request.getSession(false).getAttribute("user")==null){
				return mapping.findForward("not_login");
			}
			Blog blog = (Blog) form;
			User user = (User) request.getSession(false).getAttribute("user");
			CoreSerivceImpl serivce = new CoreSerivceImpl();
			blog.setBlog_id(user.getUsername());
			blog.setUser_id(user.getId());
			serivce.openBlog(user.getId(),blog);
			request.setAttribute("blog_id", user.getUsername());
			return mapping.findForward("openblog_sucess");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("openblog_failed");
		}
	}
	
}
